﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ComponentFactory.Krypton.Ribbon;
using ComponentFactory.Krypton.Toolkit;
using System.IO.Ports;
using System.Timers;
using System.Diagnostics;
using Steema.TeeChart.Styles;

namespace Zypt
{
    public partial class FormMain : KryptonForm
    {
        bool BufferEmpty;
        Random rnd;
        SerialPort ZySerialPort;
        int dataCount;
        int Range =2000;
        public FormMain()
        {
            InitializeComponent();
            Chn1.Caption.Text = "通道1";
            Chn2.Caption.Text = "通道2";
            Chn2.X.Color = Color.LawnGreen;
            Chn2.Y.Color = Color.LawnGreen;
            KComPort.Items.Clear();
            KComPort.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
            if (KComPort.Items.Count > 0)
            {
                KComPort.SelectedIndex = 0;
                InitSerialPort(KComPort.Text);
            }
            dataCount = 0;
            BufferEmpty = true;
            ZyData.Initialize();
             rnd= new Random();
            

        }
        private void InitSerialPort(String aComName)
        {
            if (ZySerialPort != null)
                ZySerialPort.Close();
            ZySerialPort = new SerialPort(aComName);
            ZySerialPort.BaudRate = 9600;
            ZySerialPort.Parity = Parity.None;
            ZySerialPort.StopBits = StopBits.One;
            ZySerialPort.DataBits = 8;
            ZySerialPort.Handshake = Handshake.None;

            ZySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceive_Method);

            ZySerialPort.Open();
        }
        private void DataReceive_Method(object sender, SerialDataReceivedEventArgs e)
        {
            this.Text= ZySerialPort.ReadByte().ToString();
        }
        private void kryptonSplitContainer_SizeChanged(object sender, EventArgs e)
        {
            Imp.Width = kryptonSplitContainer.Height;
            Chn1.Width = (kryptonSplitContainer.Panel1.Width-Imp.Width) / 2;
            Chn2.Width = Chn1.Width;

           
        }

        private void kryptonSplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            kryptonSplitContainer_SizeChanged(sender, e);
        }
        private void updateChart(ZyEddyTeeChart.ZyEddyXYChart chn, HorizLine imp, double[][] aData)
        {
            double[] tX = new double[Range];
            double[] tY = new double[Range];
            double[] tImpX = new double[ZyData.BufferSize];
            double[] tImpY = new double[ZyData.BufferSize];

            int[] index = new int[Range];
            chn.X.Clear();
            chn.Y.Clear();
            imp.Clear();
            if (dataCount < Range)
            {
                for (int i = 0; i < dataCount; i++)
                     index[Range - dataCount + i] = i;

                Array.Copy(aData[0], 0, tX, Range - dataCount,dataCount);
                Array.Copy(aData[1], 0, tY, Range - dataCount,dataCount);
            }
            else 
            {

                for (int i = 0; i < Range; i++)
                    index[i] = dataCount - Range + i;
                int tCountToCopy = dataCount;
                if (dataCount >= ZyData.DataSize - ZyData.BufferSize)
                    tCountToCopy = ZyData.DataSize - ZyData.BufferSize;

                Array.Copy(aData[0], tCountToCopy - Range, tX, 0, Range);
                Array.Copy(aData[1], tCountToCopy - Range, tY, 0, Range);
            }
            Array.Copy (tX, Range- ZyData.BufferSize, tImpX,0, ZyData.BufferSize);
            Array.Copy (tY, Range - ZyData.BufferSize, tImpY,0, ZyData.BufferSize);
            chn.X.Add(tX, index);
            chn.Y.Add(tY, index);
            imp.Add(tImpX, tImpY);
            if (dataCount % 100 == 0)
                SetChn1Alarm(true);
            else
                SetChn1Alarm(false);
            chn.VertAxis.Maximum = dataCount;
            chn.VertAxis.Minimum = dataCount - Range;
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            if (BufferEmpty == false)
            {
                if (dataCount >= ZyData.DataSize - ZyData.BufferSize) {                 
                    Array.Copy(ZyData.Chn1Data[0] ,ZyData.BufferSize, ZyData.Chn1Data[0], 0, ZyData.DataSize - ZyData.BufferSize);
                    Array.Copy(ZyData.Chn1Data[1], ZyData.BufferSize, ZyData.Chn1Data[1], 0, ZyData.DataSize - ZyData.BufferSize);
                    Array.Copy(ZyData.Chn2Data[0], ZyData.BufferSize, ZyData.Chn2Data[0], 0, ZyData.DataSize - ZyData.BufferSize);
                    Array.Copy(ZyData.Chn2Data[1], ZyData.BufferSize, ZyData.Chn2Data[1], 0, ZyData.DataSize - ZyData.BufferSize);
                    Array.Copy(ZyData.Position, ZyData.BufferSize, ZyData.Position, 0, ZyData.DataSize - ZyData.BufferSize);
                }
                int tCountToCopy= dataCount;
                if (dataCount >= ZyData.DataSize - ZyData.BufferSize)
                    tCountToCopy = ZyData.DataSize - ZyData.BufferSize;
                Array.Copy(ZyData.Chn1Buffer[0], 0, ZyData.Chn1Data[0], tCountToCopy, ZyData.BufferSize);
                Array.Copy(ZyData.Chn1Buffer[1], 0, ZyData.Chn1Data[1], tCountToCopy, ZyData.BufferSize);
                Array.Copy(ZyData.Chn2Buffer[0], 0, ZyData.Chn2Data[0], tCountToCopy, ZyData.BufferSize);
                Array.Copy(ZyData.Chn2Buffer[1], 0, ZyData.Chn2Data[1], tCountToCopy, ZyData.BufferSize);
                for (int i = 0; i < ZyData.BufferSize; i++)
                    ZyData.Position[tCountToCopy + i] = tCountToCopy + i;
                
                dataCount += ZyData.BufferSize;

                    BufferEmpty = true;

                updateChart(Chn1, Imp.Imp1, ZyData.Chn1Data);
                updateChart(Chn2, Imp.Imp2, ZyData.Chn2Data);
                stLbl_dataCont.Text = dataCount.ToString();
            }  
        }
        private void timer1_Tick_1(object sender, EventArgs e)
        {
            if (BufferEmpty == true)
            {
                for (int i = 0; i < ZyData.BufferSize; i++)
                {
                    double X = rnd.Next() % 5 - 2.5;
                    double Y = rnd.Next() % 5 - 2.5;
                    ZyData.Chn1Buffer[0][i] = X;
                    ZyData.Chn1Buffer[1][i] = Y;
                    ZyData.Chn2Buffer[0][i] = X + 0.5;
                    ZyData.Chn2Buffer[1][i] = Y - 0.3;

                }
                BufferEmpty = false;
            }
        }
        private void KComPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitSerialPort(KComPort.Text);
        }

        

        private void KNudPhase_ValueChanged(object sender, EventArgs e)
        {

        }

        private void KNudAlarm_ValueChanged(object sender, EventArgs e)
        {
            Imp.AlarmValue =(double)KNudAlarm.Value;
        }

        private void kryptonBtnStart_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
            timer.Enabled = !timer.Enabled;
            kryptonBtnStart.Visible = !kryptonBtnStart.Visible;
            kryptonBtnStop.Visible = !kryptonBtnStop.Visible;
            dataCount = 0;
            //Queue<double> dData;
            //dData = new Queue<double>();
            //double[] darray = new double[20000000];
            //for (int i = 0; i < 20000000 ; i++)
            //    darray[i] = i;
            //    Stopwatch watch = new Stopwatch();
            //watch.Start();
            ////for (int i = 0; i < 20000000-20; i++)
            //{
            //    darray[i] = darray[i + 20];

            //}
          //  Array.Copy(darray, 20, darray, 0, 20000000 - 20);
           // Buffer.BlockCopy(darray, 8*20, darray, 0, 8* (20000000 - 20));
          ///  watch.Stop();
            //watch.Restart();
            //dData.CopyTo(darray, 0);
            //watch.Stop();
           // this.Text = watch.ElapsedMilliseconds.ToString();

        }
        private void kryptonBtnStop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !timer1.Enabled;
            timer.Enabled = !timer.Enabled;
            kryptonBtnStart.Visible = !kryptonBtnStart.Visible;
            kryptonBtnStop.Visible = !kryptonBtnStop.Visible;

        }

        private void SetChn1Alarm(bool aAlarm)
        {
            if (aAlarm)
            {
                stLbl_Chn1Red.Visible = true;
                stLbl_Chn1Green.Visible = false;
            }
            else
            {
                stLbl_Chn1Red.Visible = false;
                stLbl_Chn1Green.Visible = true;
            }
        }
        private void SetChn2Alarm(bool aAlarm)
        {
            if (aAlarm)
            {
                stLbl_Chn2Red.Visible = true;
                stLbl_Chn2Green.Visible = false;
            }
            else
            {
                stLbl_Chn2Red.Visible = false;
                stLbl_Chn2Green.Visible = true;
            }
        }
    }
}

